﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CqusInfo.CR.SqlHelper;
using CqusInfo.RAL.User;
using System.Data.SQLite;
using System.Data;

namespace CqusInfo.DAL.User
{
    /// <summary>
    /// tb_Login
    /// </summary>
    public class LoginDAL:Interface.ILoginDAL
    {
        /// <summary>
        /// 用户登录后将信息写入
        /// </summary>
        /// <param name="model">model</param>
        /// <returns>1：成功 0：失败</returns>
        public Int32 Add(LoginModel model)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("insert into tb_Login values(");
            sqlStr.Append("@c_vc_lId,@c_vc_userName,@c_dt_dateTime)");
            SQLiteParameter[] parameters = { 
                                       new SQLiteParameter("@c_vc_lId",DbType.String),
                                       new SQLiteParameter("@c_vc_userName",DbType.String),
                                       new SQLiteParameter("@c_dt_dateTime",DbType.DateTime)
                                       };
            parameters[0].Value = model.c_vc_lId;
            parameters[1].Value = model.c_vc_userName;
            parameters[3].Value = model.c_dt_dateTime;
            parameters[3].Value = false;

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), parameters);
        }

        public dynamic Query_Login(string userName)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("select * from tb_User");
            sqlStr.Append(" where c_vc_userName=@userName");
            SQLiteParameter[] parameters ={
                                       new SQLiteParameter("@userName",DbType.String)
                                         };
            parameters[0].Value = userName;

            DataSet ds = new DataSet();
            ds = SQLiteHelper.ExecuteQuery(sqlStr.ToString(), parameters);

            DataRow row = ds.Tables[0].Rows[0];
            if (ds.Tables[0].Rows.Count != 0)
            {
                return new
                {
                    passWord = row["c_vc_passWord"].ToString(),
                    isAlive = Convert.ToBoolean(row["c_b_isAlive"]),
                    isLock = Convert.ToBoolean(row["c_b_isLock"]),
                    isDel = Convert.ToBoolean(row["c_b_isDel"])
                };
            }
            return new { error = 0 };
        }

        public Int32 IsExit(string userName, string passWord)
        {
            Int32 result = 0;
            if (Query_Login(userName).error == 0)
            {
                result = 0;
            }
            else if (Query_Login(userName).passWord != passWord)
            {
                result = 1;
            }
            else if (!(Query_Login(userName).isAlive))
            {
                result = 2;
            }
            else if (!(Query_Login(userName).isLock))
            {
                result = 3;
            }
            else if (!(Query_Login(userName).isDel))
            {
                result = 4;
            }
            else { result = 5; }
            return result;
        }
    }
}
